package Dao.Impl;

import Dao.PetStoreDao;
import Entity.pet_owner;
import Entity.pet_store;
import Util.DBUtil;

import java.sql.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class PetStoreImpl implements PetStoreDao {
    @Override
    public List<pet_store> AllGetPetStore() throws SQLException {
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM  pet_store");

        List<pet_store> pet_stores = new ArrayList<pet_store>();
        pet_store p = null;
        while(rs.next()) {
            p=new pet_store();
            p.setId(rs.getInt("id"));
            p.setName(rs.getString("name"));
            p.setPassword(rs.getString("password"));
            p.setBalance(rs.getInt("balance"));
            pet_stores.add(p);
        }

        return pet_stores;
    }

    @Override
    public pet_store AllGetPetStore(int id) throws SQLException {
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM  pet_store where id="+id);
        pet_store p = null;
        rs.next();
            p=new pet_store();
            p.setId(rs.getInt("id"));
            p.setName(rs.getString("name"));
            p.setPassword(rs.getString("password"));
            p.setBalance(rs.getInt("balance"));

        return p;
    }

    @Override
    public void add(pet_store p) throws SQLException, ParseException {
        Connection conn = DBUtil.getConnection();

        String sql = "insert into account (id, name, password, " +
                "      money)" +
                "    values (null,?,?,?)";

        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        ptmt.setString(1, p.getName());
        ptmt.setString(2, p.getPassword());
        ptmt.setInt(3, p.getBalance());

        //执行
        ptmt.execute();
    }

    @Override
    public void update(pet_store p) throws SQLException {
        Connection conn = DBUtil.getConnection();

        String sql = " update pet_store" +
                "    set name = ?," +
                "      password = ?," +
                "      balance = ?" +
                "    where id = ?";

        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        ptmt.setString(1, p.getName());
        ptmt.setString(2, p.getPassword());
        ptmt.setInt(3, p.getBalance());
        ptmt.setInt(4, p.getId());

        //执行
        ptmt.execute();
    }

    @Override
    public boolean delete(pet_store p) {
        return false;
    }

    @Override
    public pet_store Login(String name, String password) throws SQLException {
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM  pet_store where name='"+name+"'and password='"+password+"'");
        pet_store p = null;
        rs.next();
        p=new pet_store();
        p.setId(rs.getInt("id"));
        p.setName(rs.getString("name"));
        p.setPassword(rs.getString("password"));
        p.setBalance(rs.getInt("balance"));

        return p;
    }
}
